<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改图片' : '创建图片' }}
    </h2>

    <!-- 选择上传方式 -->
    <a-tabs v-model:activeKey="uploadType"
      >>
      <a-tab-pane key="file" tab="文件上传">
        <PictureUpload :picture="picture" :onSuccess="onSuccess" />
      </a-tab-pane>
      <a-tab-pane key="url" tab="URL 上传" force-render>
        <UrlPictureUpload :picture="picture" :onSuccess="onSuccess" />
      </a-tab-pane>
    </a-tabs>

    <a-form v-if="picture" layout="vertical" :model="pictureForm" @finish="handleSubmit">
      <a-form-item label="名称" name="name">
        <a-input v-model:value="pictureForm.name" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item label="简介" name="introduction">
        <a-textarea
          v-model:value="pictureForm.introduction"
          placeholder="请输入简介"
          :rows="2"
          autoSize
          allowClear
        />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          v-model:value="pictureForm.category"
          :options="categoryOptions"
          placeholder="请输入分类"
          allowClear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="pictureForm.tags"
          :options="tagOptions"
          mode="tags"
          placeholder="请输入标签"
          allowClear
        />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">创建</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import PictureUpload from '@/components/PictureUpload.vue'
import UrlPictureUpload from '@/components/UrlPictureUpload.vue'
import { onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import {
  updatePictureUsingPut,
  getPictureVoByIdUsingGet,
  listPictureTagCategoryUsingGet,
} from '@/api/pictureController.ts'
import { useRoute, useRouter } from 'vue-router'

// 存储图片对象
const picture = ref<API.PictureVO>()
// 表单对象
const pictureForm = reactive<API.PictureEditRequest>({})

/**
 * 图片上传成功的回调函数
 * @param newPicture - 上传成功后的图片对象，类型为 API.PictureVO
 */
const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
  pictureForm.name = newPicture.name
}

// 获取路由实例
const router = useRouter()

/**
 * 表单提交的处理函数
 * @param values - 表单提交的数据
 */
const handleSubmit = async (values: any) => {
  // 获取当前图片的 ID
  const pictureId = picture.value.id
  // ID 不存在，不进行后续操作
  if (!pictureId) {
    return
  }
  // 调用编辑图片的 API 方法
  const res = await updatePictureUsingPut({
    id: pictureId,
    ...values,
  })
  if (res.data.code === 0 && res.data.data) {
    message.success('创建成功')
    // 跳转到图片详情页
    router.push({
      path: `/picture/${pictureId}`,
    })
  } else {
    message.error('创建失败，' + res.data.message)
  }
}

//分类选项
const categoryOptions = ref<string[]>([])
// 标签选项
const tagOptions = ref<string[]>([])

// 获取标签和分类选项的函数
const getTagCategoryOptions = async () => {
  // 异步获取标签、分类
  const res = await listPictureTagCategoryUsingGet()

  if (res.data.code === 0 && res.data.data) {
    // 将标签列表赋值给 tagOptions
    tagOptions.value = (res.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
    // 将分类列表并赋值给 categoryOptions
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('加载选项失败，' + res.data.message)
  }
}

// 获取当前路由信息
const route = useRoute()

// 获取图片原有数据的函数
const getOldPicture = async () => {
  // 从路由参数中获取图片 ID
  const id = route.query?.id
  // 如果图片 ID 存在
  if (id) {
    // 异步获取图片信息
    const res = await getPictureVoByIdUsingGet({
      id: id,
    })

    if (res.data.code === 0 && res.data.data) {
      // 获取图片数据
      const data = res.data.data
      // 将图片数据赋值给 picture 变量
      picture.value = data
      // 将图片信息赋值给表单对象
      pictureForm.name = data.name
      pictureForm.introduction = data.introduction
      pictureForm.category = data.category
      pictureForm.tags = data.tags
    }
  }
}
// 组件挂载完成时
onMounted(() => {
  getTagCategoryOptions()
  getOldPicture()
})
</script>
<style lang="scss" scoped>
#addPicturePage {
  max-width: 720px;
  margin: 0 auto;
  .edit-bar {
    text-align: center;
    margin: 16px 0;
  }
}
</style>
